<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
class m_gmenu extends spModel{
	public $table = 'gmenu';
	public $pk = 'group_id';
	//查询菜单并缓存
	public function check(){
		$filename = APP_PATH.'/cache/group_'.$_SESSION['admin']['gid'].'.menu';
		if(file_exists($filename)){
			return unserialize(file_get_contents($filename));
		}else{
			if($_SESSION['admin']['gid'] == 0 && $_SESSION['admin']['root'] == 1){
				$myMenu = spClass('m_menu')->findAll(null,'odnum ASC,id ASC');
			}else{
				$myMenu = array();
				$menuId = $this->findAll(array('group_id'=>$_SESSION['admin']['gid']),'menu_id ASC');
				foreach($menuId as $v){
					$myMenu[] = spClass('m_menu')->find(array('id'=>$v['menu_id']));
				}
			}
			$myMenuFormat = array();
			foreach($myMenu as $v){
				$v['url'] = explode('/', $v['url']);
				$v['url'] = sprintf("?c=%s&a=%s", $v['url'][0], $v['url'][1]);
				if($v['pid'] == 0){
					if(isset($myMenuFormat[$v['id']]['child'])){
						$v['child'] = $myMenuFormat[$v['id']]['child'];
					}
					$myMenuFormat[$v['id']] = $v;
				}else{
					$myMenuFormat[$v['pid']]['child'][] = $v;
				}
			}
			file_put_contents($filename, serialize($myMenuFormat));
			return $myMenuFormat;
		}
	}
}